package com.jbit.dm_160.service.impl;

import com.jbit.dm_160.mapper.OrderMapper;
import com.jbit.dm_160.model.dto.ItemDTO;
import com.jbit.dm_160.model.dto.OrderInfoDTO;
import com.jbit.dm_160.model.dto.SchedulerSeatDTO;
import com.jbit.dm_160.model.entity.*;
import com.jbit.dm_160.model.vo.OrderVO;
import com.jbit.dm_160.service.*;
import com.jbit.dm_160.utils.Constance;
import com.jbit.dm_160.utils.DateUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class OrderServiceImpl implements OrderService {
    @Resource
    private UserServiceImpl userService;
    @Resource
    private ItemService itemService;
    @Resource
    private OrderMapper orderMapper;
    @Resource
    private SchedulerSeatService schedulerSeatService;
    @Resource
    private LinkUserService linkUserService;
    @Resource
    private OrderLinkUserService orderLinkUserService;

    @Transactional
    @Override
    public String addOrder(OrderVO orderVO,String token) {
        String [] linkIdArray = getArrayByLinkIds(orderVO.getLinkIds());
        List<Integer> positionList = getPositionsBySeatPositions(orderVO.getSeatPositions());
        List<SchedulerSeat> schedulerSeatList = new ArrayList<>();
        for (int i = 0; i < positionList.size(); i+=2) {
            SchedulerSeat schedulerSeat = new SchedulerSeat();
            schedulerSeat.setScheduleId(orderVO.getSchedulerId());
            schedulerSeat.setX(positionList.get(i));
            schedulerSeat.setY(positionList.get(i+1));
            schedulerSeatList.add(schedulerSeat);
        }
        List<SchedulerSeatDTO> schedulerSeatDTOS = schedulerSeatService.getScheduleSeatPriceByInfo(orderVO.getSchedulerId(),schedulerSeatList);
        double totalPrice = 0;
        for (SchedulerSeatDTO schedulerSeatDTO : schedulerSeatDTOS) {
            totalPrice += schedulerSeatDTO.getPrice();
        }

        User user = userService.getUserByToken(token);
        ItemDTO itemDTO = itemService.getItemDetailById(orderVO.getItemId());
        Order order = new Order();
        BeanUtils.copyProperties(orderVO,order);
        order.setInvoiceNo(orderVO.getInvoiceId());
        order.setOrderNo(DateUtils.getDateString());
        order.setUserId(user.getId());
        order.setItemName(itemDTO.getItemName());
        order.setOrderType(0);
        order.setTotalCount(linkIdArray.length);
        //是否需要保险
        if (order.getIsNeedInsurance() == 1){
            order.setInsuranceAmount(20D);
            order.setTotalAmount(totalPrice + 20);
        }else{
            order.setTotalAmount(totalPrice);
        }
        //只是添加了订单,还需要添加订单详情之类的
        orderMapper.addOrder(order);
        //开始添加订单详情
        //获取订单详情需要添加的用户信息
        List<LinkUser> linkUserList = linkUserService.getLinkUsersByIds(linkIdArray);
        for (int i = 0; i < linkUserList.size(); i++) {
            SchedulerSeatDTO schedulerSeatDTO = schedulerSeatDTOS.get(i);
            LinkUser linkUser = linkUserList.get(i);
            OrderLinkUser orderLinkUser = new OrderLinkUser();
            orderLinkUser.setOrderId(order.getId());
            orderLinkUser.setLinkUserId(linkUser.getId());
            orderLinkUser.setLinkUserName(linkUser.getName());
            orderLinkUser.setX(schedulerSeatDTO.getX());
            orderLinkUser.setY(schedulerSeatDTO.getY());
            orderLinkUser.setPrice(schedulerSeatDTO.getPrice());
            orderLinkUserService.addOrderLinkUser(orderLinkUser);
        }
        schedulerSeatService.updateSeatStatus(orderVO.getSchedulerId(),schedulerSeatList, Constance.STATUS_UN_PAY_,order.getOrderNo());
        return order.getOrderNo();

    }

    @Override
    public OrderInfoDTO getOrderInfoByOrderNo(String orderNo) {
        return orderMapper.getOrderInfoByOrderNo(orderNo);
    }

    @Override
    public List<OrderInfoDTO> getOrderInfos(OrderVO orderVO) {
        return orderMapper.getOrderInfos(orderVO);
    }

    @Override
    public Integer updateOrderByAlipay(Order order) {
        return orderMapper.updateOrderByAlipay(order);
    }

    public String[] getArrayByLinkIds(String linkIds){
        String [] strs = linkIds.split(",");
        return strs;
    }

    public List<Integer> getPositionsBySeatPositions(String seatPositions){
        List<Integer> list = new ArrayList();
        //遍历两个值 第一个值是 x  第二个值是y
//        list.add(1);
//        list.add(3);
//        list.add(1);
//        list.add(4);
        //1_3和,1_4
        String [] strs = seatPositions.split(",");
        for (String str : strs) {
            //1 和 3
            //1 和 4
            String[] infos = str.split("_");
            for (String info : infos) {
                list.add(Integer.parseInt(info));
            }
        }
        return list;
    }

}
